package com.huluobo.a2208mdmdemo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2208mdmdemo.ui.theme._2208MDMDemoTheme
import com.huluobo.a2208mdmdemo.view.WelcomePage
import com.huluobo.a2208mdmdemo.view.mainPage
import com.huluobo.apps.AppsPage
import com.huluobo.mvidemo.news.NewsPage
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            _2208MDMDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    var startPage = "welcome"
                    val mmkv = MMKV.defaultMMKV()
                    val navController = rememberNavController()
                    //去读是否进入过app
                    val isIn = mmkv.decodeBool("isIn", false)
                    if (isIn) {
                        startPage = "main"
                    } else {
                        lifecycleScope.launch {
                            async { delay(5000) }.await()
                            navController.navigate("main")
                            //保存是否进入欢迎页的状态
                            mmkv.encode("isIn", true)
                        }
                    }

                    NavHost(navController = navController, startDestination = startPage, builder = {
                        composable("welcome") { WelcomePage() }
                        composable("main") { mainPage() }
                    })
//                    AppsPage()
                }
            }
        }
    }
}